const { defineConfig } = require('@vue/cli-service')
const path = require('path')
// const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin')
const NodePolyfillPlugin =  require('node-polyfill-webpack-plugin')
// const UglifyPlugin =require('uglifyjs-webpack-plugin')
const webpack = require('webpack')
module.exports = defineConfig({
  publicPath: '/',
  outputDir: 'dist',
  lintOnSave: false,
  devServer: {
    // open: process.platform === 'darwin',
    host: '0.0.0.0',
    port: 9000,
    proxy: {
      '/api': {
        target: 'http://106.12.241.246:180',
       // target: 'http://192.168.3.24:8222',
        // target: 'http://192.168.3.69:8222',
        changeOrigin: true,
        ws: false,    // 关闭sockjs
        pathRewrite: {
          '^/api': ''
        }
      }
    },
    client: {
      overlay: {
        runtimeErrors: false
      }
    }
  },
  chainWebpack: (config) => {
    config.module.rule('vue').use('vue-loader').tap(options => {
      return options
    })
  },
  configureWebpack: (config) => {
    const plugins = [
      new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery",
        "windows.jQuery": "jquery",
      }),
    ]
    plugins.push(new NodePolyfillPlugin({
      excludeAliases: ['console']
    }))
    if (process.env.NODE_ENV === 'production') {
      config.mode = 'production'
     // config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
      // let optimization = {
      //   runtimeChunk: 'single',
      //   minimizer: [new UglifyPlugin({
      //     uglifyOptions: {
      //       warnings: false,
      //       compress: {
      //         drop_console: true,
      //         drop_debugger: false,
      //         pure_funcs: ['condole.log']
      //       }
      //     }
      //   })],
      //   splitChunks: {
      //     chunks: 'all',
      //     maxInlineSize: Infinity,
      //     minSize: 20000,
      //     cacheGroups: {
      //       vendor: {
      //         test: /[\\/]node_modules[\\/]/,
      //         name(module) {
      //           const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
      //           return `npm.${packageName.replace('@', '')}`
      //         }
      //       }
      //     }
      //   }
      // }
      // Object.assign(config, {
      //   plugins: [
      //     new AddAssetHtmlPlugin(Object.keys(library).map(name => {
      //       return {
      //         filepath: require.resolve(path.resolve(`libs/package/js/${name}.${version_lib}.dll.js`)),
      //         outputPath: 'static/lib/js',
      //         publicPath: './static/lib/js',
      //         includeSourcemap: false
      //       }
      //     }))
      //   ],
      //   optimization
      // })
    } else {
      config.mode = 'development'
    }
    Object.assign(config, {
      resolve: {
        alias: {
          '@': path.resolve(__dirname, './src'),
          crypto: false,
          stream: false,
          asserts: false,
          http: false,
          https: false
        }
      }
    })
  },
  productionSourceMap: false,
  transpileDependencies: true,
  css: {
    extract: true,
    sourceMap: false,
    loaderOptions: {
      sass: {
        data: `@import "@/assets/scss/common.scss";`
      }
    }
  },
  parallel: require('os').cpus().length > 1
})
